﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Net;
using System.IO;
using System.Collections;
using System.Text.RegularExpressions;

namespace OnlineMusic
{
    class BaiduLrc : IComparer<LrcText>
    {
        private const string Url = "http://box.zhangmen.baidu.com/x?op=12&count=1&title=";
        public static string FindLrc(string title, string singer)
        {
            string URL = Url + Uri.EscapeUriString(title) + "$$" + Uri.EscapeUriString(singer) + "$$$$";
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
            request.Method = WebRequestMethods.Http.Get;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            XmlDocument doc = new XmlDocument();
            XmlElement result = null;
            string lrcurl = null;
            try
            {
                doc.Load(response.GetResponseStream());
                result = doc["result"];
                if (!result["count"].InnerText.Equals("0"))
                {
                    XmlElement url = result["url"];
                    int lrcid = Convert.ToInt32(url["lrcid"].InnerText);
                    if (lrcid > 0)
                    {
                        lrcurl = "http://box.zhangmen.baidu.com/bdlrc/" + lrcid / 100 + "/" + lrcid + ".lrc";
                    }
                }
            }
            finally
            {
            }
            response.Close();
            return lrcurl;
        }

        public static string LoadLrc(string lrcurl)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(lrcurl);
            request.Method = WebRequestMethods.Http.Get;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
            string lrc = reader.ReadToEnd();
            reader.Close();
            response.Close();
            return lrc;
        }

        public static List<LrcText> ReadLrc(string lrc)
        {
            List<LrcText> lrcs = new List<LrcText>();
            StringReader reader = new StringReader(lrc);
            string tmp = "";

            do
            {
                tmp = reader.ReadLine();
                if (tmp == null)
                    break;
                Regex reg = new Regex("\\d\\d:\\d\\d\\.\\d\\d");
                MatchCollection matches = reg.Matches(tmp);
                Regex regtext = new Regex("\\[\\d\\d:\\d\\d\\.\\d\\d\\]");
                string text = regtext.Replace(tmp, "");
                foreach (Match match in matches)
                {
                    LrcText lt = new LrcText();
                    lt.Time = ToTime(match.Value);
                    lt.Text = text;
                    lrcs.Add(lt);
                }
            } while (tmp != null);
            lrcs.Sort(new BaiduLrc());
            return lrcs;
        }

        private static int ToTime(string timestr)
        {
            string m = timestr.Substring(0, 2);
            string s = timestr.Substring(3, 2) + timestr.Substring(6);
            return Convert.ToInt16(m) * 60000 + Convert.ToInt16(s) * 10;
        }

        public int Compare(LrcText a, LrcText b)
        {
            return a.Time.CompareTo(b.Time);
        }
    }

    class LrcText
    {
        public int Time = 0;
        public string Text = "";
    }
}
